# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore, QtGui
from ui_mainWindow import Ui_MainWindow

aboutOriginDBText = """
					<h3> About OriginDB </h3>
					<p> OriginDB is a program for connecting to the Origin of Life Chemical Database
					developed by Jason Power (<a href=mailto:power.jg@gmail.com>power.jg@gmail.com</a>) at
					<a href=http://www.gatech.edu/>Georgia Institute of Techonolgy</a> for
					The Origins Project (<a href=http://www.theoriginscenter.net/>http://www.theoriginscenter.net/</a>).</p>
					<p> For more information check the project website
					<a href=http://origindb.googlecode.com/>http://origindb.googlecode.com/</a>.</p>
					<p> OriginDB is <A href="http://www.gnu.org/philosophy/free-sw.html">free software</a>
					and available under the <a href=http://www.gnu.org/licenses/gpl-2.0.html>GPLv2</a>.</p>
					<p><i>Copyright 2010 - Jason Power</i></p>
					"""

class MainWindow(QtGui.QMainWindow):
	def __init__(self, controller):
		QtGui.QMainWindow.__init__(self)

		self.controller = controller

		self.editFrame = None
		self.searchFrame = None
		self.detailsFrame = None

        # Set up the user interface from Designer.
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

		# process for qt assistant
		self.proc = None

		self.mainSplitter = QtGui.QSplitter()

		self.horizSplitter = QtGui.QSplitter(QtCore.Qt.Vertical)

		self.mainSplitter.addWidget(self.horizSplitter)

		self.setCentralWidget(self.mainSplitter)

		self.compoundMenu = QtGui.QMenu("Compound")
		self.actionAddNewCompound = self.compoundMenu.addAction("Add New Compound", self.controller.newCompoundAction)
		self.actionSearchCompound = self.compoundMenu.addAction("Search Compound", self.controller.searchCompoundAction)
		self.actionSubstructSearch = self.compoundMenu.addAction("Substructure Search", self.controller.subSearchAction)
		self.actionImportCompound = self.compoundMenu.addAction("Import PubChem Compound", self.controller.importCompoundAction)

		self.reactionMenu = QtGui.QMenu("Reaction")
		self.actionAddNewReaction = self.reactionMenu.addAction("Add New Reaction", self.controller.newReactionAction)

		self.referenceMenu = QtGui.QMenu("Reference")
		self.actionAddNewReference = self.referenceMenu.addAction("Add New Reference", self.controller.newReferenceAction)
		self.actionSearchReference = self.referenceMenu.addAction("Search Reference", self.controller.searchReferenceAction)
		self.actionImportReference = self.referenceMenu.addAction("Import Reference", self.controller.importReferenceAction)

		self.viewMenu = QtGui.QMenu("View")
		self.actionInformation = self.viewMenu.addAction("View Information", self.controller.informationAction)
		self.actionViewSearch = self.viewMenu.addAction("View Search", self.controller.viewSearchAction)
		self.actionViewVisual = self.viewMenu.addAction("View Pathway Visualization", self.controller.viewVisualization)
		self.actionInformation.setCheckable(True)
		self.actionInformation.setChecked(False)
		self.actionViewSearch.setCheckable(True)
		self.actionViewSearch.setChecked(False)
		#self.actionDetails.setCheckable(True)
		#self.actionDetails.setChecked(False)

		self.menuBar().clear()
		self.menuBar().addMenu(self.ui.fileMenu)
		self.menuBar().addMenu(self.viewMenu)
		self.menuBar().addMenu(self.referenceMenu)
		self.menuBar().addMenu(self.compoundMenu)
		self.menuBar().addMenu(self.reactionMenu)
		self.menuBar().addMenu(self.ui.helpMenu)

		self.connect(self.ui.actionConnect, QtCore.SIGNAL("triggered(bool)"), self.controller.loginAction)
		self.connect(self.ui.actionLogout, QtCore.SIGNAL("triggered(bool)"), self.controller.logoutAction)
		self.connect(self.ui.actionPassword, QtCore.SIGNAL("triggered(bool)"), self.controller.passwordAction)
		self.connect(self.ui.actionUserInfo, QtCore.SIGNAL("triggered(bool)"), self.controller.userInfoAction)
		self.connect(self.viewMenu, QtCore.SIGNAL("aboutToShow()"), self.updateViewMenu)
		self.connect(self.ui.fileMenu, QtCore.SIGNAL("aboutToShow()"), self.updateFileMenu)

		self.connect(self.ui.actionAbout_Qt, QtCore.SIGNAL("triggered()"), lambda : QtGui.QMessageBox.aboutQt(self))
		self.connect(self.ui.actionOriginDB_Help, QtCore.SIGNAL("triggered()"), self.help)
		self.connect(self.ui.actionAbout_OriginDB, QtCore.SIGNAL("triggered()"), lambda : QtGui.QMessageBox.about(self, "About OriginDB", aboutOriginDBText))


	def viewEditFrame(self, view=False):
		if view or self.actionInformation.isChecked():
			self.actionInformation.setChecked(True)
			self.editFrame.show()
		else:
			self.actionInformation.setChecked(False)
			self.editFrame.hide()

	def viewSearchFrame(self, view=False):
		if view or self.actionViewSearch.isChecked():
			try:
				self.searchFrame.show()
				self.actionViewSearch.setChecked(True)
			except AttributeError:
				self.actionViewSearch.setChecked(False)
		else:
			self.actionViewSearch.setChecked(False)
			self.searchFrame.hide()

	def viewDetailsFrame(self, view=False):
		if view or self.actionDetails.isChecked():
			try:
				self.detailsFrame.show()
				self.actionDetails.setChecked(True)
			except AttributeError:
				self.actionDetails.setChecked(False)
		else:
			self.actionDetails.setChecked(False)
			self.detailsFrame.hide()


	def setEditFrame(self, frame):
		"""Sets the frame passed in as the edit frame.
		must already be in the splitter by calling
		addEditFrames"""

		self.editFrame.hide()
		self.editFrame = frame
		self.editFrame.show()
		self.viewEditFrame(True)


	def addEditFrames(self, *listOfFrames):
		"""Adds a list of frames to the splitter at the
		right location. Shows the first one added"""
		for frame in listOfFrames:
			self.mainSplitter.insertWidget(0, frame)
			frame.hide()

		self.editFrame = listOfFrames[0]
		self.editFrame.show()
		self.viewEditFrame(True)

	def setSearchFrame(self, frame):
		self.searchFrame = frame
		loc = self.horizSplitter.indexOf(frame)
		if loc != -1:
			self.horizSplitter.widget(loc).show()
			return

		self.horizSplitter.insertWidget(0, frame)

		self.viewSearchFrame(True)


	def setDetailsFrame(self, frame):
		self.detailsFrame = frame
		loc = self.horizSplitter.indexOf(frame)
		if loc != -1:
			self.horizSplitter.widget(loc).show()
			return

		if self.horizSplitter.count > 0:
			self.horizSplitter.insertWidget(1, frame)
		else:
			self.horizSplitter.insertWidget(0, frame)

		self.viewDetailsFrame(True)

	def updateViewMenu(self):
		if self.searchFrame.isVisible():
			self.actionViewSearch.setChecked(True)
		else:
			self.actionViewSearch.setChecked(False)
		if self.editFrame.isVisible():
			self.actionInformation.setChecked(True)
		else:
			self.actionInformation.setChecked(False)

	def updateFileMenu(self):
		if not self.controller.userLoggedIn():
			self.ui.actionPassword.setEnabled(False)
			self.ui.actionUserInfo.setEnabled(False)
		else:
			self.ui.actionPassword.setEnabled(True)
			self.ui.actionUserInfo.setEnabled(True)

	def help(self):
		if not self.proc or not self.proc.state == self.proc.Running:
			self.proc = QtCore.QProcess()
			app = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.BinariesPath)+"/assistant"

			self.proc.start(app, ["-collectionFile", "docs/collection.qhc", "-enableRemoteControl"])
			if not self.proc.waitForStarted():
				QtGui.QMessageBox.critical(self, "Remote Control", "Could not start Qt Assistant from "+app)
				return